<?php 
// +----------------------------------------------------------------------
// | ThinkPHP                                                             
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.      
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>                                  
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * DirectoryIterator实现类 PHP5以上内置了DirectoryIterator类
 +------------------------------------------------------------------------------
 * @category   ORG
 * @package  ORG
 * @subpackage  Io
 * @author    liu21st <liu21st@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */
//不支持读出Visual FoxPro 3.0或更高的版本
class readdbf
{
  var $isutf8;//是否转换成UTF8
  var $file;//文件路径
  var $field;//纪录的字段
  var $fieldname;
  var $fieldlen;//一条纪录的长度
  var $head;//文件头部长度
  var $fieldnum;
  
  function readdbf($file,$isutf8)
  {	
  	global $codetable;
 	if(file_exists($file)){
		$this->file=$file;
	}else{
		echo "文件路径无效!";
		exit();
	}
/*	//一个纪录的长度
	$fieldlen=0;
	foreach($field as $tmp){
		$fieldlen=$fieldlen+$tmp;
	}
	$this->field=$field;
	$this->fieldlen=$fieldlen;*/
	$this->isutf8=$isutf8;
	//计算文件的头部长度
	$fd=fopen($this->file,"rb");
	rewind($fd);
/*	$tmp=0;
	$str=0;
	while(ord($str)!=13){
		fseek($fd,$tmp);
		$str=fread($fd,1);
		$tmp++;
	}*/
	fseek($fd,8);
	$str1=ord(fread($fd,1));
	fseek($fd,9);
	$str2=ord(fread($fd,1));
	$this->head=$str1+$str2*256;
	
	//echo $this->head/16;
	//echo "<br>";
	//echo "$str1 $str2";
	
	fseek($fd,10);
	$str1=ord(fread($fd,1));
	fseek($fd,11);
	$str2=ord(fread($fd,1));
	$this->fieldlen=$str1+$str2*256;
	
	//echo "<br>";
	//echo $this->fieldlen;
	//echo "<br>";
	//echo "$str1 $str2";
	
	//读出字段名
	$i=32;
	$n=0;	
	while(($i+10)<$this->head){
		fseek($fd,$i);
		$str=chop(fread($fd,10));
		if ($str=="") break;
		//echo "$str<br>";

		/*编码转换，gbk转UTF-8*/
		if($this->isutf8)
		{
			$this->fieldname[$n]=iconv('gbk','utf-8',$str);
		}else{
			$this->fieldname[$n]=$str;
		}
		fseek($fd,$i+16);
		$strlen=ord(fread($fd,1));
		//echo "$strlen<br>";
		$this->field[]=$strlen;
		
		$i=$i+32;
		$n++;
	}
//print_r($field);
//$field=array(20,4,10);
//print_r($field);
		
	
	fseek($fd,4);
	$str1=ord(fread($fd,1));
	fseek($fd,5);
	$str2=ord(fread($fd,1));
	$this->fieldnum=$str1+$str2*256;	
	
	fclose($fd);
	//
	//exit();
  }
  
  function read()
  {
  global $codetable;
  	//dbf文件头部长度
	$field=$this->field;
	//$head=96;
	//$head=392;
	//得到文件长度
	//$filesize=filesize($this->file);
	//打开文件
	$fd=fopen($this->file,"rb");
	//文件指针移动开头
	rewind($fd);
	$offset=$this->head;
	//$fieldnum=($filesize-$this->head-1)/($this->fieldlen+1);
	//echo $filesize."<br>";
	//echo $this->head."<br>";
	//echo $this->fieldlen."<br>";
	//echo $fieldnum."<br>";
	for($num=1;$num<=$this->fieldnum;$num++){
		//echo $offset."<br>";
		$offset++;
		for($i=0;$i<count($field);$i++){
			fseek($fd,$offset);
			if ($this->isutf8==0){
				$arrtmp[$this->fieldname[$i]]=chop(fread($fd,$field[$i]));
				//echo $arrtmp[$i];
			}elseif($this->isutf8==1){
				$arrtmp[$this->fieldname[$i]]=chop(iconv('gbk','utf-8',fread($fd,$field[$i])));
			}
			//echo "<br>".$field[$i]."<br>";
			$offset+=$field[$i];
		}
		//去除空白数据
		$empty=0;
		foreach ($arrtmp as $arrstr){
			if (empty($arrstr)) $empty++;
		}
		if($empty!=count($arrtmp))$arr[]=$arrtmp;	
	}
	fclose($fd);
	return $arr;
  }
}
/*
//dbf字段长度
$field=array(20,30,4,10);
$dbf=new readdbf("ce.dbf",$field,1);
$arr=$dbf->read();
foreach ($arr as $tmp){
	echo $tmp[0]."-".$tmp[1]."-".$tmp[2]."<br>";
}
*/	



class outdbf
{
  var $isutf8;//是否转换成UTF8
  var $file;//文件路径
  var $field;//纪录的字段
  var $fieldsize;
  var $fieldname;
  var $fieldlen;//一条纪录的长度
  function outdbf($file,$field,$fieldsize,$fieldname,$isutf8)
  {
  	$this->file=$file;
	$this->fieldsize=$fieldsize;
	$this->isutf8=$isutf8;
	$this->fieldname=$fieldname;
	if($this->isutf8==1){
		for($k=0;$k<count($this->fieldname);$k++){
			$this->fieldname[$k]=iconv('utf-8','gbk',$this->fieldname[$k]);
		}
	}
	for($k=0;$k<count($this->fieldname);$k++){
		if(strlen($this->fieldname[$k])>10)	$this->fieldname[$k]=substr($this->fieldname[$k],0,10);
	}
	//一个纪录的长度+1
	$fieldlen=1;
	foreach($fieldsize as $tmp){
		$fieldlen=$fieldlen+$tmp;
	}
	$this->field=$field;
	$this->fieldlen=$fieldlen;

	//计算文件的头部长度
	$fieldlen1=$fieldlen%256;
	$fieldlen2=$fieldlen/256;
	//echo $fieldlen;
	
	$all=(count($fieldsize)+1)*32+1;	
	$all1=$all%256;
	$all2=$all/256;
	$num1=count($field)%256;
	$num2=count($field)/256;
	$head1=chr(03).chr(07).chr(07).chr(10).chr($num1).chr($num2).chr(00).chr(00);
	$head2=chr($all1).chr($all2).chr($fieldlen1).chr($fieldlen2).chr(00).chr(00).chr(00).chr(00);
	$head3=chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00);
	$head4=chr(00).chr(00).chr(00).chr(00).chr(00).chr(0x7a).chr(00).chr(00);
	$fp=fopen($this->file,"w");
	fwrite($fp,$head1);
	fwrite($fp,$head2);
	fwrite($fp,$head3);
	fwrite($fp,$head4);
	$fieldnum=count($fieldsize);
	$field_offset=1;
	for($i=0;$i<$fieldnum;$i++){
		
		fwrite($fp,$this->fieldname[$i]);
		for($n=strlen($this->fieldname[$i]);$n<11;$n++) fwrite($fp,chr(00));
		
		fwrite($fp,"C".chr($field_offset).chr(00).chr(00).chr(00));
		$field_offset=$field_offset+$fieldsize[$i];
		fwrite($fp,chr($fieldsize[$i]).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00));
		fwrite($fp,$head3);
	}
	//文件头部结束符
	fwrite($fp,chr(0x0d));
	//print_r($field);
	foreach($field as $tmp){
		fwrite($fp,chr(0x20));
		for($i=0;$i<$fieldnum;$i++){
			//输出数据
			if($this->isutf8==1) fwrite($fp,iconv('utf-8','gbk',$tmp[$i]));
			else fwrite($fp,$tmp[$i]);
			//fwrite($fp,$tmp[$i]);
			//补空
			if($this->isutf8==1) for($n=strlen(iconv('utf-8','gbk',$tmp[$i]));$n<$fieldsize[$i];$n++) fwrite($fp,chr(0x20));
			else for($n=strlen($tmp[$i]);$n<$fieldsize[$i];$n++) fwrite($fp,chr(0x20));
		}		
	}
	//数据结束符
	fwrite($fp,chr(0x1a));
	//foreach 
	fclose($fp);
	//返回文件名
  }
}
class outcsv
{
  var $isutf8;//是否转换成UTF8
  var $file;//文件路径
  var $field;//纪录的字段
  var $fieldname;
  function outcsv($file,$field,$fieldname,$isutf8)
  {
  	$this->file=$file;
	$this->fieldname=$fieldname;
	$this->field=$field;
	$this->isutf8=$isutf8;

	$fp=fopen($this->file,"w");
	$str="";
	foreach($this->fieldname as $tmp){
		$str.=$tmp.",";
	}
	$str=substr($str,0,strlen($str)-1)."\r\n";
	fwrite($fp,$str);
	
	foreach($field as $tmp){
		$str="";
		foreach($tmp as $tmp1){
			$str.=$tmp1.",";
		}
		$str=substr($str,0,strlen($str)-1)."\r\n";
		fwrite($fp,$str);
	}
	//foreach 
	fclose($fp);
	//
  }
}
/*
$field=array(array("2005551033","无问林","男"),array("2005551034","房磊","男"));
$fieldsize=array(20,30,2);
$fieldname=array("XH","XM","XB");
$outfile=new outdbf("out.dbf",$field,$fieldsize,$fieldname,1);
*/

?>